﻿/*
Discio
Copyright (c) 2013 - Walter M. Soto Reyes
Microsoft Public License (Ms-PL)
License Agreement:  https://discio.codeplex.com/license
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq; 

namespace Discio
{
    public class SiteSources : IEnumerable
    {
        private static volatile SiteSources _instance;
        private static object _syncRoot = new Object();

        public SiteSources()
        {
            _sources = new Dictionary<string, Source>();
        }

        private Dictionary<string, Source> _sources;

        public Source Default
        {
            get
            {
                if (_sources.Count > 0)
                {
                    return _sources.FirstOrDefault().Value;
                }
                return new Source();
            }
        }

        public bool Contains(string key)
        {
            return _sources.ContainsKey(key);
        }

        public bool IsEmpty { get { return _sources.Count == 0; } }

        public Source this[string key]
        {
            set
            {

                if (_sources.ContainsKey(key))
                {
                    _sources[key] = value;
                }
                else
                {
                    _sources.Add(key, value);
                }
            }

            get
            {
                return _sources.ContainsKey(key) ? _sources[key] : new Source();
            }
        }
        public void Reset()
        {
            _sources = new Dictionary<string, Source>();
        }

        public static SiteSources Source
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncRoot)
                    {
                        if (_instance == null)
                        {
                            _instance = new SiteSources();
                        }
                    }
                }

                return _instance;
            }
        }

        #region IEnumerable Members


        public IEnumerator GetEnumerator()
        {
            return (_sources.Values as IEnumerable).GetEnumerator();
        }


        #endregion

    }
}
